require("../../assets/css/normalize.css");
require("../../assets/css/reset.css");
require("../../assets/fonts/iconfont.css");
require("../../assets/css/tabbar.less");
require("./index.less");
// 引入swiper
require("../../libs/swiper/swiper-bundle.css");
const Swiper = require("../../libs/swiper/swiper-bundle");
//引入模块
const dom = require("../../utils/dom.js");
const axios = require("axios");
document.addEventListener("DOMContentLoaded", function () {
	// 渲染tabbar
	dom.tabbar("index");
	var mySwiper = new Swiper(".swiper", {
		loop: true, // 循环模式选项

		//等同于以下设置
		autoplay: {
			delay: 2000,
			stopOnLastSlide: false,
			disableOnInteraction: false,
		},

		// 如果需要分页器
		pagination: {
			el: ".swiper-pagination",
			clickable: true,
		},
		grabCursor: true,
		effect: "creative",
		creativeEffect: {
			prev: {
				shadow: true,
				origin: "left center",
				translate: ["-5%", 0, -200],
				rotate: [0, 100, 0],
			},
			next: {
				origin: "right center",
				translate: ["5%", 0, -200],
				rotate: [0, -100, 0],
			},
		},
	});
	//发请求获取用户信息
	let token = localStorage.getItem("token");
	axios.defaults.headers.common["Authorization"] = "Bearer " + token;
	axios.get("http://fitness.h5.itsource.cn:3701/api/user/info").then((res) => {
		let data = res.data.data;
		dom.get("#rankNum").textContent = data.ranking;
		//获取徽章
		dom.get("#badege-data").textContent = data.badges;
	});
	//获取打卡信息
	function clockrun() {
		axios
			.get("http://fitness.h5.itsource.cn:3701/api/user/clockInInfo")
			.then((res) => {
				dom.get("#clockNum").textContent = res.data.data.clockCount;
				let btn = dom.get(".rankbtu");
				if (res.data.data.isClockIn) {
					btn.disabled = true;
					btn.textContent = "已打卡";
				}
			});
	}
	clockrun();
	//绑定打卡事件
	dom.get(".rankbtu").addEventListener("click", function () {
		axios
			.post("http://fitness.h5.itsource.cn:3701/api/user/clockIn")
			.then((res) => {
				if (res.data.errno === 0) {
					clockrun();
				}
			});
	});
});
